package com.dw.etl.core.springbatch.processor;

import com.dw.etl.common.base.mapper.BaseMapper;
import com.dw.etl.core.api.EtlRemindService;
import com.dw.etl.core.mapper.MdMedTbFldMapper;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * Created by liujunlei on 2021/6/15.
 */
@Component
@StepScope
public class DimHandleProcessor implements ItemProcessor<Map<String,Object>,Map<String,Object>> {

    private static String FLD_SRC_HOS = "pk_dim_hos";
    @Autowired
    EtlRemindService etlRemindService;
    @Autowired
    MdMedTbFldMapper mdMedTbFldMapper;
    @Autowired
    BaseMapper baseMapper;

    @Value("#{jobParameters['dtEnd']}")
    private Date dtEnd;

    @Value("#{jobParameters['dtBegin']}")
    private Date dtBegin;



    /**
     * 从事实表中查询维度字段，并通过字段与维表的对照关系获得维表
     * 如果维度需要对照执行与对照表的关联，如果不需要直接与维表关联
     * 关联不上的说明该维度值在维度字典里不存在，发提醒。
     * @param stringObjectMap
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> process(Map<String, Object> stringObjectMap) throws Exception {
        /*String tableName = stringObjectMap.get("idMedtb").toString();
        MdMedTbFd tbFdModel = new MdMedTbFd();
        tbFdModel.setIdMedTb(tableName);
        tbFdModel.setFgAct((byte)1);
        List<MdMedTbFd> tbFdList = mdMedTbFldMapper.findByModel(tbFdModel);
        BaseDimtb baseDimtb;
        String dimFld,dimTable,dimSrcTable,filter;
        StringBuffer sqlBuffer = new StringBuffer("select count(*) from "+tableName);
        List<Map<String,Object>> dataMapList;
        for(MdMedTbFd tbFd:tbFdList) {
            dimFld = tbFd.getPkPubfd().toLowerCase();
            if(dimFld.startsWith(CommonConstant.PRE_DIM)) {
                baseDimtb = baseDimtbService.getByIdFromCache(dimFld);
                if(baseDimtb.getFgComp()==1) {
                    dimSrcTable = baseDimtb.getNaDimcomptb();
                    filter = baseDimtb.getFilter();
                    sqlBuffer.append(" left join "+dimSrcTable+" on "+tableName+"."+dimFld+"="+dimSrcTable+"."+this.FLD_SRC_HOS);
                    if(StringUtils.isNotEmpty(baseDimtb.getFilter())) {
                        sqlBuffer.append(" and "+tableName+"."+filter+"="+dimSrcTable+"."+filter);
                    }
                } else {
                    dimTable = baseDimtb.getNaDimtb();
                    filter = baseDimtb.getFilter();
                    sqlBuffer.append(" left join "+dimTable+" on "+tableName+"."+dimFld+"="+dimTable+".pk");
                    if(StringUtils.isNotEmpty(baseDimtb.getFilter())) {
                        sqlBuffer.append(" and "+tableName+"."+filter+"="+dimTable+"."+filter);
                    }
                }
                sqlBuffer.append(" where "+tableName+"."+dimFld+" is null and "+CommonConstant.DT_SYS_CRE+"<"+dtEnd+" and "+CommonConstant.DT_SYS_CRE+">="+dtBegin);
                dataMapList = baseMapper.findBySql(sqlBuffer.toString());
                if(dataMapList.size()>0) {
                    etlRemindService.insert(tableName+"表中维度"+dimFld+"出现新值",(byte)2,"");
                }
            }
        }*/
        return null;
    }
}
